﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Threading;
using System.IO.IsolatedStorage;
using System.Diagnostics;

namespace ControlHost
{
    public partial class SettingPage : PhoneApplicationPage
    {
        IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

        //public TcpSocket tcpSock;
        public UdpSocket udpSock;
        public string strHost;
        public int nPort;
        public byte[] data;

        public SettingPage()
        {
            InitializeComponent();

            data = new byte[64];
        }

        private void saveHost(string host)
        {
            if (settings.Contains("Host"))
            {
                settings["Host"] = host;
            }
            else
            {
                settings.Add("Host", host);
            }
        }

        private void savePort(int port)
        {
            if (settings.Contains("Port"))
            {
                settings["Port"] = port;
            }
            else
            {
                settings.Add("Port", port);
            }
        }

        private void btnConfirm_Click(object sender, RoutedEventArgs e)
        {
            strHost = tbHost.Text;
            nPort = Convert.ToInt32(tbPort.Text);
            (Application.Current as App).strHost = strHost;
            (Application.Current as App).nPort = nPort;
            saveHost(strHost);
            savePort(nPort);
            //tcpSock.Connect(strHost, nPort);
            Debug.WriteLine("{0}, {1}", (Application.Current as App).strHost, (Application.Current as App).nPort);

        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            //tcpSock = (Application.Current as App).tcpSock;
            udpSock = (Application.Current as App).udpSock;
            strHost = (Application.Current as App).strHost;
            nPort = (Application.Current as App).nPort;
            tbHost.Text = strHost;
            tbPort.Text = Convert.ToString(nPort);

            base.OnNavigatedTo(e);
        }
    }
}